<template>
    <div class="common-layout">
        <el-row>
            <el-select v-model="state.selected" class="m-2" placeholder="日期" 
            @change="change"
            size="large">
                <el-option v-for="item in state.options" :key="item" :label="item" :value="item">
                    {{ item }}
                </el-option>
            </el-select>

            <el-select v-model="state.industrySelected" class="m-2" placeholder="行业" 
            filterable
            @change="industryChange"
            size="large">
                <el-option v-for="item in state.industryTags" :key="item" :label="item" :value="item" />
            </el-select>

            <el-text> 数量：{{ state.data.length }} </el-text>

        </el-row>

        <el-row>
            <el-table :data="state.data" height="700" 
            :cell-class-name="tableCellClassName"
            @sort-change="sortChange"
            @cell-click="industryClick"
            :default-sort="state.sort"
            style="width: 100%">
                <el-table-column prop="date" label="日期" width="210" />
                <el-table-column prop="category" label="类别"
                    :filter-method="filterCategory"
                    :filters="[
                        { text: '股票', value: '股票' },
                        { text: 'ETF', value: 'ETF' },
                        { text: '指数', value: '指数' },
                    ]"
                />
                <el-table-column prop="stock" label="股票" width="150">
                    <template #default="scope">
                        <ToStockDetail :stock="scope.row.stock" />
                    </template> 
                </el-table-column>
                <el-table-column prop="display" label="display" width="100"> 
                    <template #default="scope">
                        <to-display :stock="scope.row.stock" :date="scope.row.date" ></to-display>
                    </template>
                </el-table-column>
                <el-table-column prop="close" label="价格"/>
                <el-table-column prop="industry" label="行业" sortable width="150"/>
                <el-table-column prop="lp" label="分位点" sortable width="100" />
                <el-table-column prop="relLP" label="相对分位点" sortable width="100" />
                <el-table-column prop="weekPct" label="周涨跌" sortable  width="100"/>
                <el-table-column prop="nextWeekPct" label="下周涨跌" sortable  width="100"/>
                <el-table-column prop="gb20wPct" label="20周线" sortable  width="100" />
                <el-table-column prop="gb30wPct" label="30周线" sortable  width="100" />
                <el-table-column prop="disPct" label="60周线"  sortable width="100" />
                <el-table-column prop="disDuration" label="disDuration"  sortable width="100" />
                <el-table-column prop="incNum" label="连续上涨" sortable width="100" />
                <el-table-column prop="pctSum" label="涨跌和" sortable width="100" >
                    <template #default="scope">
                        {{ scope.row.pctSum && (scope.row.pctSum).toFixed(2) }}
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
    </div>
</template>

<script setup>
import { reactive } from 'vue';
import { getWeekMeanGT } from '../service/week'
import ToStockDetail from './ToStockDetail.vue';
import { getLpClassName, getSessionStorage, setSessionStorage } from '../utils/help';
import ToDisplay from './element/ToDisplay.vue';

const base = "WeekMeanGt"
const selectKey = base + "Select"
const industrySelectKey = base + "Industry" + "Select"
const sortPropKey = base + "SortProp"
const sortOrderKey = base + "SortOrder"

const state = reactive({
    rawData: [],
    data : [],
    options: [],
    industryTags: new Set(),
    industrySelected: getSessionStorage(industrySelectKey) || "全部",
    selected: getSessionStorage(selectKey) || "",
    sort: { 
        prop: getSessionStorage(sortPropKey) || 'weekPct' , 
        order: getSessionStorage(sortOrderKey) || 'ascending' 
    }
})

const loadData = (selected) => {
    getWeekMeanGT(selected).then( res => {
        state.rawData = res.data.data
        state.options = res.data.date
        if (!selected) {
            state.selected = state.options[0]
        }

        state.data = state.rawData

        state.industryTags.add("全部")
        state.rawData.forEach(item => {
            state.industryTags.add(item.industry)
        })
        industryChange()
    })
}

loadData(state.selected)

        const sortChange = (column, prop, order) => {
            setSessionStorage(sortPropKey, column.prop)
            setSessionStorage(sortOrderKey, column.order)
        }

        const filterCategory = (value, row) => {
            return row.category === value
        }

        const change = () => {
            setSessionStorage(selectKey, state.selected)
            loadData(state.selected)
        }

        const industryChange = () => {
            if (state.industrySelected == "全部") {
                state.data = state.rawData
            } else {
                state.data = state.rawData.filter(item => item.industry == state.industrySelected)
            }
            setSessionStorage(industrySelectKey, state.industrySelected)
        }

        const industryClick = (row, column, cell, event) => {
            var columnProperty = column.property
            if (columnProperty == "industry") {
                // 还原
                if (state.industrySelected === row.industry) {
                    state.industrySelected = "全部"
                } else {
                    state.industrySelected = row.industry
                }
                industryChange()
            }
        }

        const tableCellClassName = ({row, column, rowIndex, columnIndex}) => {
            var columnProperty = column.property
            if ((columnProperty == "lp")) {
                return getLpClassName(row.lp)
            }

            if (columnProperty == "relLP") {
                if (row["relLP"] <= 1) {
                    return 'rellplt1-cell'
                }
            }

            if (["weekPct", "nextWeekPct"].includes(columnProperty)) {
                if (row[columnProperty] < 0) {
                    return 'lt0-cell'
                }
            }

            if (["w15DisPct", "disPct","gb20wPct","gb30wPct"].includes(columnProperty)) {
                if (row[columnProperty] >= 0) {
                    return 'd300up-cell'
                } else {
                    return 'd300down-cell'
                }
            }

            if (["上证指数","创业板","上证50","沪深300","科创50","中证500","中证1000","中证全指"].includes(row.stock)) {
                return 'lp-warning-row'
            }

            return ''
        }
</script>

<style>
</style>